import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/LoginPage.vue')
    },
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/novel',
      component: () => import('@/views/home/index.vue'),
      children: [
        {
          path: '/novel/:id',
          component: () => import('@/views/novel/novelDetail.vue')
        }
      ]
    },
    {
      path: '/createNovel',
      component: () => import('@/views/novel/CreateNovel.vue')
    },
    {
      path: '/updateNovel/:id',
      component: () => import('@/views/novel/UpdateNovel.vue')
    },
    {
      path: '/createChapter/:id',
      component: () => import('@/views/chapter/CreateChapter.vue')
    },
    {
      path: '/updateChapter',
      component: () => import('@/views/chapter/UpdateChapter.vue')
    }
  ]
})
// 路由守卫
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
  return true
})
export default router
